package org.dreamwork.jasmine2.sample.page;

import org.dreamwork.jasmine2.events.EventException;
import org.dreamwork.jasmine2.sample.ColorItem;
import org.dreamwork.jasmine2.web.controls.Page;
import org.dreamwork.jasmine2.web.controls.UserControl;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: seth.yang
 * Date: 13-10-8
 * Time: 下午3:39
 */
public class Part1 extends UserControl {
    protected List<ColorItem> items;
    protected List<String> columnNames;
    protected List<Map<String, String>> data;

    public Part1 () {
        items = Arrays.asList (
            new ColorItem ("Red", "#f00"), new ColorItem ("Green", "#0f0"), new ColorItem ("Blue", "#00f")
        );

        columnNames = Arrays.asList ("Id", "Name", "Age", "Status");

        data = new ArrayList<Map<String, String>> ();
        for (int i = 0; i < 10; i ++) {
            Map<String, String> map = new HashMap<String, String> ();
            map.put ("Id", UUID.randomUUID ().toString ());
            map.put ("Name", "User " + (i + 1));
            map.put ("Age", String.valueOf ((int) (Math.random () * 50 + 20)));
            map.put ("Status", String.valueOf (Math.random () > .5));
            data.add (map);
        }
    }

    @Override
    public void onPageLoadCompleted (Page page) throws EventException {
        try {
            dataBind ();
        } catch (Throwable throwable) {
            throw new EventException (throwable);
        }
    }
}